package com.zhuo.project.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuo.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhuo.framework.web.domain.AjaxResult;
import com.zhuo.project.order.mapper.ZzDiagnoseDetailMapper;
import com.zhuo.project.order.domain.ZzDiagnoseDetail;
import com.zhuo.project.order.service.IZzDiagnoseDetailService;

/**
 * 处方单明细Service业务层处理
 * 
 * @author zhuo
 * @date 2020-03-26
 */
@Service
public class ZzDiagnoseDetailServiceImpl implements IZzDiagnoseDetailService 
{
    @Autowired
    private ZzDiagnoseDetailMapper zzDiagnoseDetailMapper;

    /**
     * 查询处方单明细
     * 
     * @param id 处方单明细ID
     * @return 处方单明细
     */
    @Override
    public ZzDiagnoseDetail selectZzDiagnoseDetailById(Long id)
    {
        return zzDiagnoseDetailMapper.selectZzDiagnoseDetailById(id);
    }

    @Override
    public List<ZzDiagnoseDetail> selectZzDiagnoseDetailByDiagnoseId(String diagnoseId) {
        ZzDiagnoseDetail zzDiagnoseDetail = new ZzDiagnoseDetail();
        zzDiagnoseDetail.setDiagnoseId(diagnoseId);
        return zzDiagnoseDetailMapper.selectZzDiagnoseDetailList(zzDiagnoseDetail);
    }

    /**
     * 查询处方单明细列表
     * 
     * @param zzDiagnoseDetail 处方单明细
     * @return 处方单明细
     */
    @Override
    public List<ZzDiagnoseDetail> selectZzDiagnoseDetailList(ZzDiagnoseDetail zzDiagnoseDetail)
    {
        return zzDiagnoseDetailMapper.selectZzDiagnoseDetailList(zzDiagnoseDetail);
    }

    @Override
    public List<ZzDiagnoseDetail> selectZzDiagnoseDetailListByOrderMaterialId(String diagnoseId, String materialId) {
        ZzDiagnoseDetail zzDiagnoseDetailSearch = new ZzDiagnoseDetail();
        zzDiagnoseDetailSearch.setDiagnoseId(diagnoseId);
        zzDiagnoseDetailSearch.setMaterialId(materialId);
        List<ZzDiagnoseDetail> list = selectZzDiagnoseDetailList(zzDiagnoseDetailSearch);
        return list;
    }

    @Override
    public Map<String, Double> getDiagnoseLeftQty(String diagnoseId) {
        ZzDiagnoseDetail zzDiagnoseDetailSearch = new ZzDiagnoseDetail();
        zzDiagnoseDetailSearch.setDiagnoseId(diagnoseId);
        List<ZzDiagnoseDetail> list = selectZzDiagnoseDetailList(zzDiagnoseDetailSearch);
        Map<String, Double> diagnoseLeftQtyMap = new HashMap<>(list.size());
        for (ZzDiagnoseDetail detail :list) {
            String materialId = detail.getMaterialId();
            Double itemNoLeftQty = detail.getQty() - detail.getTotalSendQty();
            if (diagnoseLeftQtyMap.containsKey(materialId)) {
                diagnoseLeftQtyMap.put(materialId, diagnoseLeftQtyMap.get(materialId) + itemNoLeftQty);
            }else{
                diagnoseLeftQtyMap.put(materialId, itemNoLeftQty);
            }
        }
        return diagnoseLeftQtyMap;
    }


    @Override
    public Double getDiagnoseMaterialIdLeftQty(String diagnoseId, String materialId) {
        ZzDiagnoseDetail zzDiagnoseDetail = new ZzDiagnoseDetail();
        zzDiagnoseDetail.setDiagnoseId(diagnoseId);
        zzDiagnoseDetail.setMaterialId(materialId);
        List<ZzDiagnoseDetail> list = this.selectZzDiagnoseDetailList(zzDiagnoseDetail);
        double leftSendQty = 0.0;
        if (null == list || list.size() == 0) {
            return leftSendQty;
        }

        for (ZzDiagnoseDetail detail : list) {
            leftSendQty = leftSendQty + detail.getQty() - detail.getTotalSendQty();
        }
        return leftSendQty;
    }

    /**
     * 新增处方单明细
     * 
     * @param zzDiagnoseDetail 处方单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertZzDiagnoseDetail(ZzDiagnoseDetail zzDiagnoseDetail)
    {
        zzDiagnoseDetail.setCreateTime(DateUtils.getNowDate());
        return zzDiagnoseDetailMapper.insertZzDiagnoseDetail(zzDiagnoseDetail);
    }

    /**
     * 修改处方单明细
     * 
     * @param zzDiagnoseDetail 处方单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateZzDiagnoseDetail(ZzDiagnoseDetail zzDiagnoseDetail)
    {
        zzDiagnoseDetail.setUpdateTime(DateUtils.getNowDate());
        return zzDiagnoseDetailMapper.updateZzDiagnoseDetail(zzDiagnoseDetail);
    }

    /**
     * 批量删除处方单明细
     * 
     * @param ids 需要删除的处方单明细ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteZzDiagnoseDetailByIds(Long[] ids)
    {
        return zzDiagnoseDetailMapper.deleteZzDiagnoseDetailByIds(ids);
    }

    /**
     * 删除处方单明细信息
     * 
     * @param id 处方单明细ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteZzDiagnoseDetailById(Long id)
    {
        return zzDiagnoseDetailMapper.deleteZzDiagnoseDetailById(id);
    }
}
